<?php

/**
 * 73YC 提交用户评论
 * ============================================================================
 * * 版权所有 20013-* 梅州络客网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.alork.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2013-10-19 06:29:08Z liubo $
*/

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
require(ROOT_PATH . 'includes/cls_json.php');
include('region_select.php');
if (!isset($_REQUEST['cmt']) && !isset($_REQUEST['act']))
{
    /* 只有在没有提交评论内容以及没有act的情况下才跳转 */
    ecs_header("Location: ./\n");
    exit;
}
$_REQUEST['cmt'] = isset($_REQUEST['cmt']) ? json_str_iconv($_REQUEST['cmt']) : '';
$json   = new JSON;
$result = array( 'message' => '','error'=>'');
if (empty($_REQUEST['act']))
{
    $cmt  = $json->decode($_REQUEST['cmt']);
        // 查询订单是否已经评论
    $order_id=$cmt->order_id;//订单ID
    $sql = "SELECT comment FROM ".$GLOBALS['ecs']->table('order_info')." WHERE `order_id` =".$order_id." AND `order_status` ='5' AND `shipping_status` ='2' AND `pay_status` ='2'";
    $comment= $db->getRow($sql);
    $comment=$comment["comment"];
    if($comment=="2"){
        $result["error"]="你已经评论2次了亲";
    }
    elseif ($comment=="1") {
        up_comment($cmt);
        $result["message"]="追加评论成功";
    }
    else{
        add_comment($cmt);
        $result["message"]="评论成功";
    }
    echo $json->encode($result);
    exit;
}
else
{
    /*
     * act 参数不为空
     * 默认为评论内容列表
     * 根据 _GET 创建一个静态对象
     */
    $cmt = new stdClass();
    $cmt->id   = !empty($_GET['id'])   ? intval($_GET['id'])   : 0;
    $cmt->type = !empty($_GET['type']) ? intval($_GET['type']) : 0;
    $cmt->page = isset($_GET['page'])   && intval($_GET['page'])  > 0 ? intval($_GET['page'])  : 1;
}

if ($result['error'] == 0)
{
    $comments = assign_comment($cmt->id, $cmt->type, $cmt->page);

    $smarty->assign('comment_type', $cmt->type);
    $smarty->assign('id',           $cmt->id);
    $smarty->assign('username',     $_SESSION['user_name']);
    $smarty->assign('email',        $_SESSION['email']);
    $smarty->assign('comments',     $comments['comments']);
    $smarty->assign('pager',        $comments['pager']);
    $smarty->assign('rate',         $comments['rate']);

    /* 验证码相关设置 */
    if ((intval($_CFG['captcha']) & CAPTCHA_COMMENT) && gd_version() > 0)
    {
        $smarty->assign('enabled_captcha', 1);
        $smarty->assign('rand', mt_rand());
    }

    $result['message'] = $_CFG['comment_check'] ? $_LANG['cmt_submit_wait'] : $_LANG['cmt_submit_done'];
    $result['content'] = $smarty->fetch("library/comments_list.lbi");
}

echo $json->encode($result);
exit;
/*------------------------------------------------------ */
//-- PRIVATE FUNCTION
/*------------------------------------------------------ */

/**
 * 添加评论内容
 *
 * @access  public
 * @param   object  $cmt
 * @return  void
 */
function add_comment($cmt)
{
    /* 评论是否需要审核 */
    $status = 1 - $GLOBALS['_CFG']['comment_check'];
    $user_id = empty($_SESSION['user_id']) ? 0 : $_SESSION['user_id'];
    $user_name = empty($cmt->username) ? $_SESSION['user_name'] : '';
    $user_name = htmlspecialchars($user_name);
    $order_id=$cmt->order_id;//订单ID
    $goods_id=$cmt->goods_id;//主商品ID
    $goods_comment=$cmt->goods_comment;//主商品评价
    $goods_rand=$cmt->goods_rand;//主商品评分
    $service_comment=$cmt->service_comment;//服务评价
    $service_rank=$cmt->service_rank;//服务评分
    $send_rank=$cmt->send_rank;//发货评分
    $logistic_rand=$cmt->logistic_rand;//物流评分
    /* 保存评论内容 */
               empty($goods_comment)?$goods_comment="好评！":$goods_comment=$goods_comment;
    $sql = "INSERT INTO " .$GLOBALS['ecs']->table('comment') .
           "( goods_id,order_id,user_name, content, comment_rank, add_time, ip_address, status, parent_id, user_id) VALUES " .
           "('" .$goods_id. "', '$order_id','$user_name', '" . $goods_comment."', '".$goods_rand."', ".gmtime().", '".real_ip()."', '$status', '0', '$user_id')";
    $result = $GLOBALS['db']->query($sql);
    //如果有评价其他产品
        if(!empty($cmt->goods_next)){
    $next=$cmt->goods_next;//其他产品内容
    foreach ($next as $key => $value) {
            empty($value[1])?$value[1]="好评！":$value[1]=$value[1];
            $sql = "INSERT INTO " .$GLOBALS['ecs']->table('comment') .
           "( goods_id,order_id,user_name, content, comment_rank, add_time, ip_address, status, parent_id, user_id) VALUES " .
           "('" .$value[0]. "','$order_id', '$user_name', '" .$value[1]."', '".$value[2]."', ".gmtime().", '".real_ip()."', '$status', '0', '$user_id')";
            $result = $GLOBALS['db']->query($sql);
    }
        }
    /* 保存订单评价分数 */
    $sql = "UPDATE " .$GLOBALS['ecs']->table('order_info') ."SET `comment_con`='".$service_comment."',`comment_rank1`='".$service_rank."',`comment_rank2`='".$send_rank."',`comment_rank3`='".$logistic_rand."',`comment`='1' WHERE `order_id`='".$order_id."'";
    $result = $GLOBALS['db']->query($sql);
    clear_cache_files('comments_list.lbi');
    /*if ($status > 0)
    {
        add_feed($GLOBALS['db']->insert_id(), COMMENT_GOODS);
    }*/
    return $result;
}

/**
 * 追加评论内容
 *
 * @access  public
 * @param   object  $cmt
 * @return  void
 */
function up_comment($cmt)
{
    /* 评论是否需要审核 */
    $status = 1 - $GLOBALS['_CFG']['comment_check'];
    $user_id = empty($_SESSION['user_id']) ? 0 : $_SESSION['user_id'];
    $user_name = empty($cmt->username) ? $_SESSION['user_name'] : '';
    $user_name = htmlspecialchars($user_name);
    $order_id=$cmt->order_id;//订单ID
    //如果有评价其他产品
        if(!empty($cmt->goods_next)){
    $next=$cmt->goods_next;//追加评论
    foreach ($next as $key => $value) {
            empty($value[1])?$value[1]="好评！":$value[1]=$value[1];
             $sql = "UPDATE " .$GLOBALS['ecs']->table('comment') ."SET `up_time`='".gmtime()."',`content_more`='".$value[1]."' WHERE `order_id`='".$order_id."' AND `goods_id`='".$value[0]."'";
            $result = $GLOBALS['db']->query($sql);     
    }
        }
    /* 保存订单评价分数 */
    $sql = "UPDATE " .$GLOBALS['ecs']->table('order_info') ."SET `comment`='2' WHERE `order_id`='".$order_id."'";
    $result = $GLOBALS['db']->query($sql);
    clear_cache_files('comments_list.lbi');
    return $result;
}
?>